package com.framework.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.system.entity.dto.SysMenuAddDTO;
import com.framework.system.entity.dto.SysMenuEditDTO;
import com.framework.system.entity.dto.SysMenuQueryDTO;
import com.framework.system.entity.vo.RouteItemVO;
import com.framework.system.entity.vo.SysMenuListVO;
import com.framework.system.entity.vo.SysMenuVO;
import com.framework.system.service.ISysMenuService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表  前端控制器
 * </p>
 *
 * @author Kevin
 * @since 2021-06-19
 */
@RestController
@RequestMapping("/system/sysMenu")
@AllArgsConstructor
public class SysMenuController {

    private ISysMenuService sysMenuService;

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    @SaCheckPermission("system:sysMenu:page")
    @AutoLog(module = "菜单管理", title = "分页查询")
    public R<Page<SysMenuListVO>> selectPage(Page reqPage, SysMenuQueryDTO req) {
        return R.success(sysMenuService.selectPage(reqPage, req));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @SaCheckPermission("system:sysMenu:add")
    @AutoLog(module = "菜单管理", title = "新增")
    public R<Void> add(@Validated @RequestBody SysMenuAddDTO req) {
        sysMenuService.add(req);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    @SaCheckPermission("system:sysMenu:edit")
    @AutoLog(module = "菜单管理", title = "修改")
    public R<Void> edit(@Validated @RequestBody SysMenuEditDTO req) {
        sysMenuService.edit(req);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @SaCheckPermission("system:sysMenu:remove")
    @AutoLog(module = "菜单管理", title = "删除")
    public R<Void> remove(@RequestParam String ids) {
        sysMenuService.remove(ids);
        return R.success();
    }

    /**
     * 查看
     */
    @GetMapping("/view")
    @SaCheckPermission("system:sysMenu:view")
    @AutoLog(module = "菜单管理", title = "查看")
    public R<SysMenuVO> view(@RequestParam String id) {
        return R.success(sysMenuService.view(id));
    }

    /**
     * 查询菜单树
     */
    @GetMapping("/listMenuTree")
    @AutoLog(module = "菜单管理", title = "查询菜单树")
    public R<List<Tree<String>>> listMenuTree() {
        return R.success(sysMenuService.listMenuTree());
    }

    /**
     * 查询下拉菜单树，排除当前节点以及子节点
     */
    @GetMapping("/selectMenuTreeAndExcludeNode")
    @AutoLog(module = "菜单管理", title = "查询下拉菜单树，排除当前节点以及子节点")
    public R<List<Tree<String>>> selectMenuTreeAndExcludeNode(@RequestParam(required = false) String nodeId) {
        return R.success(sysMenuService.selectMenuTreeAndExcludeNode(nodeId));
    }

    /**
     * 查询下拉菜单树
     */
    @GetMapping("/selectMenuTree")
    @AutoLog(module = "菜单管理", title = "查询下拉菜单树")
    public R<List<Tree<String>>> selectMenuTree() {
        return R.success(sysMenuService.selectMenuTree());
    }

    /**
     * 获取菜单集合
     */
    @GetMapping("/getMenuList")
    @AutoLog(module = "菜单管理", title = "获取菜单集合")
    public R<List<RouteItemVO>> getMenuList() {
        return R.success(sysMenuService.getMenuList());
    }

}

